package com.lovo.util;

import com.lovo.pojo.UserDO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GetConnectionUtil {
    private Connection con;
    private PreparedStatement statement;
    public Connection getConnection() {
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/sqlzoo?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "20001202");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return con;
    }

    // 对数据库数据进行修改（增、删、改）
    public void insert(String name, String psw, double money) {
        // 1、 创建链接
        Connection con = getConnection();
        Statement statement = null;
        try {
            // 2、获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "INSERT INTO t_bank(b_username,b_userpsw,b_money) values ('" + name + "'," + psw + ",'" + money + "')";
            // 3、执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4、关闭连接
            close(con, statement);
        }
    }

    // 修改
    public void insert(String name, double money) {
        // 1、 创建链接
        Connection con = getConnection();
        Statement statement = null;
        try {
            // 2、获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "update t_bank set b_money='"+ money +"' where b_username='" + name + "'";
            // 3、执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4、关闭连接
            close(con, statement);
        }
    }

    public void insert(String name, String psw) {
        // 1、 创建链接
        Connection con = getConnection();
        Statement statement = null;
        try {
            // 2、获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "update t_bank set b_userpsw='"+ psw +"' where b_username='" + name + "'";
            // 3、执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4、关闭连接
            close(con, statement);
        }
    }

    // 查询
    public List query() {
        // 1、 创建链接
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        List userList = new ArrayList();
        try {
            // 2、获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "select * from t_bank";
            // 3、执行SQL语句
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                UserDO user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setUsername(rs.getString("b_username"));
                user.setUserpsw(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4、关闭连接
            close(con, statement, rs);
        }
        return userList;
    }

    public void insert(UserDO user) {
        // 1、 创建链接
        Connection con = getConnection();
        String sql = "INSERT INTO t_bank(b_username,b_userpsw,b_money) values(?,?,?)";
        try {
            // 2、获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1,user.getUsername());
            statement.setObject(2,user.getUserpsw());
            statement.setObject(3,user.getMoney());
            // 3、执行SQL语句
            int row = statement.executeUpdate();
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4、关闭连接
            close(con, statement);
        }
    }

    public List query(String name) {
        // 1、 创建链接
        Connection con = getConnection();
        ResultSet rs = null;
        List studentList = new ArrayList();
        String sql = "select * from t_bank where b_username=?";
        try {
            // 2、获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1,name);
            // 3、执行SQL语句
            rs = statement.executeQuery();
            while (rs.next()) {
                UserDO user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setUsername(rs.getString("b_username"));
                user.setUserpsw(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
                studentList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4、关闭连接
            close(con, statement, rs);
        }
        return studentList;
    }

    public void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(){
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void setAutoCommit(boolean autoCommit){
        try {
            con.setAutoCommit(autoCommit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void commit(){
        try {
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void rollback(){
        try {
            con.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 根据用户名和密码查询用户对象
     * @param username
     * @param pwd
     * @return
     */
    public UserDO getByUsernameAndPwd(String username,String pwd){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_bank where b_username=? and b_userpsw=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,username);
            statement.setObject(2,pwd);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setUsername(rs.getString("b_username"));
                user.setUserpsw(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public UserDO getById(int id){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_bank where b_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,id);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setUsername(rs.getString("b_username"));
                user.setUserpsw(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据用户名查询用户对象
     * @param username
     * @return
     */
    public UserDO getByUsername(String username){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_bank where b_username=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,username);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setUsername(rs.getString("b_username"));
                user.setUserpsw(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据User对象中的数据进行修改
     * @param user
     */
    public void update(UserDO user){
        // 1. 创建连接
//        Connection con = getConnection();
//        PreparedStatement statement = null;
        String sql = "update t_bank set b_userpsw=?,b_money=? where b_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1,user.getUserpsw());
            statement.setObject(2,user.getMoney());
            statement.setObject(3,user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
//            close(con,statement);
        }
    }
}
